<?php

	// +----------------------------------------------------------------------
	// | 分类模型
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\model\cms;


	use app\model\file\Image;

    class Category extends Common
	{
		public static $style_list = [1 => '频道封面', 2 => '终极列表', 3 => '单页'];
		public static $allow_publish = [0 => '不允许', 1 => '允许'];
		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onBeforeInsert($data)
		{
			$data['status'] = 1;
		}

        public function cover()
        {
            return $this->belongsTo(Image::class,'cover_id');
        }
		//关联模型
		//分类对应多个文档
		public function documents()
		{
			return $this->hasMany('Document');
		}

		public function parent()
		{
			return $this->belongsTo(Category::class, 'pid');
		}

		public function children()
		{
			return $this->hasMany(Category::class, 'pid');
		}


		/**
		 * 对应多个
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/7/2 6:13
		 */
		public function patterns()
		{
			return $this->belongsToMany(Pattern::class, CategoryPattern::class);
		}

		//获取器
		public function getStyleTextAttr($value, $data)
		{
			return self::$style_list[$data['style']];
		}

		public function getAllowPublishTextAttr($value)
		{
			return self::$allow_publish[$value];
		}

		public function getFormatTitleAttr($value, $data)
		{
			if (0 == $data['pid']) {
				return $data['title'];
			} else {
				return '   ├' . $data['title'];
			}
		}


	}
